package TUILayer;

import ControlLayer.*;
import ModelLayer.*;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;

/**
 * Write a description of class OrdreUI here.
 * 
 * @author (Gruppe 1) 
 * @version (17.12.2012)
 */
public class OrdreUI
{
    // instance variables - replace the example below with your own
    private OrdreCtr ordreCtr;
    private OrdreContainer ordreContainer;
    private Ansat ansat;
    private ArrayList<Kunde> kundeListe;
    private Ordre ordre;

    /**
     * Constructor for objects of class OrdreUI
     */
    public OrdreUI()
    {
        ordreCtr = OrdreCtr.getInstance();
    }

   /**
    * Start menuen
    */
    public void start()
    {
        clr();
        OrdreMenu();
        }
   /**
    * Starter ordre menuen
    */
    private void OrdreMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeOrdreMenu();
            switch(choice)
            {
                case 1:
                    createOrdre();
                    break;
                case 2:
                    showOrdreListe();
                    break;
                case 3:
                    exit = true;
                    break;
                default:
                    break;
            }
        }
    }
    /**
     * Teksten i ordre menuen.
     */
    private int writeOrdreMenu()
    {
        print(" *** Ordre Modul *** ");
        print(" (1) Opret nyt Salg");
        print(" (2) Vis liste over Salg");
        print(" (3) Gaa tilbage til hovedmenu");
        print("\n Foretag din beslutning: ");
            
        boolean exit = false;
        int choise = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                choise = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt valg, proev igen");
            }
        }
        return choise;
    }
    /**
     * Lav en ordre.
     */
    private void createOrdre()
    {
        oneSpace();
        ArrayList<Kunde> kundeListe  = ordreCtr.getKundeListe();
        for(Kunde k : kundeListe)
        {
            print("ID: "  + k.getId() + " Fornavn: " + k.getFnavn() + " Efternavn: " + k.getEnavn());
        }
        oneSpace();
        int id = getInputInt("Indtast id nummeret paa kunden:");
        Kunde kunde = null;
        try
        {
            kunde = (Kunde)ordreCtr.getPerson(id);;
        }
        catch(Exception e)
        {
        }

    }
    /**
     * Vis ordre liste.
     */
    private void showOrdreListe()
    {
        oneSpace();
        ArrayList<Ordre> ordreListe = ordreContainer.getOrdreListe();
        for(Kunde k : kundeListe)
        {
            print("ID: " + ordre.getId());
            print("Kunde:     ID: " + k.getId() + " Fornavn: " + k.getFnavn() + " Efternavn: " + k.getEnavn());
            print("Ekspedien: ID: " + ansat.getLogin() + " Fornavn: " + ansat.getEnavn());
            print("Købs Dato: " + ordre.getDato()); 
        }
        oneSpace();
    }
    /**
     * Start et input
     * 
     * @param String str - Lavet et input
     */
    private int getInputInt(String str)
    {
        print(str);
        
        boolean exit = false;
        int integer = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                integer = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return integer;
    }
    
    /**
     * Resetter menuen.
     */
    private void clr()
    {
        print("\f");
    }
    /**
     * Print String str
     */
    private void print(String str)
    {
        System.out.println(str);
    }
    /**
     * Print oneSpace
     */
    private void oneSpace()
    {
        print("\n");
    }
}
